package com.candy.cms.controller;


import com.candy.cms.entity.ArticleVo;
import com.candy.common.result.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.candy.cms.entity.ArticleContent;
import com.candy.cms.service.IArticleContentService;
import io.swagger.annotations.Api;

/**
 * <p>
 * 文章内容 前端控制器
 * </p>
 *
 * @author pxs
 * @since 2021-06-27
 */
@Api(tags="文章内容")
@RestController
@RequestMapping("/cms/article-content")
public class ArticleContentController {

	@Autowired
	private IArticleContentService articleContentService;

	@ApiOperation(value = "详情")
	@GetMapping(value = "/{id}")
	public R getInfo(@PathVariable Long id)
	{
		return R.success(articleContentService.getArticleById(id));
	}

	@ApiOperation(value = "新增")
	@PostMapping
	public R save(@Validated @RequestBody ArticleVo articleVo)
	{
		ArticleContent articleContent = articleVo.getArticleContent();
		articleContent.setArticleId(articleVo.getId());
		return R.success(articleContentService.save(articleContent));
	}

	@ApiOperation(value = "修改")
	@PutMapping
	public R edit(@Validated @RequestBody ArticleVo articleVo)
	{
		ArticleContent articleContent = articleVo.getArticleContent();
		if(articleContent.getId()!=null)
		{
			return R.success(articleContentService.updateById(articleContent));
		}
		else
		{
			articleContent.setArticleId(articleVo.getId());
			return R.success(articleContentService.save(articleContent));
		}
	}
}
